package pers.xuankai.connectmysql.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pers.xuankai.connectmysql.service.UserService;
import pers.xuankai.springboot.utils.JsonResult;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService service;

    @GetMapping
    public Object get(Integer id){
        return new JsonResult<>(id == null
        ? service.getAllUser()
        : service.getUser(id));
    }

    @PostMapping("add")
    public Object add(String name, int birthYear, String sex){
        if(service.addUser(name, birthYear, sex) == 1){
            return new JsonResult<>();
        }else{
            return new JsonResult<>(200, "添加失败，请检查参数");
        }
    }
}
